/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[MenuComponent.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package swing.components;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;

/**
 * Swing菜单相关组件
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class MenuComponent {

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				MenuFrame frame = new MenuFrame();
				frame.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}
		});
	}
}

/**
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class MenuFrame extends JFrame {

	private static final long serialVersionUID = -4032096242801710662L;
	private static final int DEFAULT_WIDTH = 350;
	private static final int DEFAULT_HEIGHT = 200;

	private JCheckBoxMenuItem readonlyItem = null;
	private JPopupMenu popupMenu = null;

	private Action saveAction = null;
	private Action saveAsAction = null;

	@SuppressWarnings("serial")
	public MenuFrame() {
		setTitle("Menu Test");
		this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		setLocationByPlatform(true);
		setResizable(false);

		// 创建"文件"菜单和"新建"菜单项
		JMenu fileMenu = new JMenu("File");
		fileMenu.add(new MenuAction("New"));

		// 创建"打开"菜单项并关联快捷键Ctrl + O
		JMenuItem openItem = fileMenu.add(new MenuAction("Open"));
		openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));

		// 添加分隔符
		fileMenu.addSeparator();

		// 创建"保存"菜单项
		saveAction = new MenuAction("Save");
		JMenuItem saveItem = fileMenu.add(saveAction);
		saveItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));

		// 创建"另存为"菜单项
		saveAsAction = new MenuAction("Save as");
		fileMenu.add(saveAsAction);

		// 添加分隔符
		fileMenu.addSeparator();

		// 创建退出菜单项
		fileMenu.add(new AbstractAction("Exit") {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		// 只读选菜单项 - 设置只读时禁用"保存"和"另存为"
		readonlyItem = new JCheckBoxMenuItem("Read-Only");
		readonlyItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				boolean saveOk = !readonlyItem.isSelected();
				saveAction.setEnabled(saveOk);
				saveAsAction.setEnabled(saveOk);
			}
		});

		// 设置单选按钮菜单项
		ButtonGroup buttonGroup = new ButtonGroup();

		JRadioButtonMenuItem insertItem = new JRadioButtonMenuItem("Insert");
		insertItem.setSelected(true);// 设置默认选中
		JRadioButtonMenuItem deleteItem = new JRadioButtonMenuItem("Delete");
		buttonGroup.add(insertItem);
		buttonGroup.add(deleteItem);

		// 创建"编辑"菜单
		JMenu editMenu = new JMenu("Edit");
		Action cutAction = new MenuAction("Cut");
		Action copyAction = new MenuAction("Copy");
		Action pasteAction = new MenuAction("Paste");
		editMenu.add(cutAction);
		editMenu.add(copyAction);
		editMenu.add(pasteAction);

		// 创建选项子菜单
		JMenu optionMenu = new JMenu("Option");
		optionMenu.add(readonlyItem);
		optionMenu.addSeparator();
		optionMenu.add(insertItem);
		optionMenu.add(deleteItem);

		editMenu.addSeparator();
		editMenu.add(optionMenu);

		// 创建"帮助"菜单
		JMenu helpMenu = new JMenu("Help");
		// 设置快捷键
		helpMenu.setMnemonic('H');

		// 创建"关于"菜单项
		JMenuItem aboutItem = new JMenuItem("About", 'A');
		// aboutItem.setMnemonic('A');
		helpMenu.add(aboutItem);

		// 创建"捐助"菜单
		Action donateItem = new MenuAction("Donate");
		donateItem.putValue(Action.MNEMONIC_KEY, new Integer('D'));
		helpMenu.add(donateItem);

		// 构建菜单栏
		JMenuBar menuBar = new JMenuBar();
		menuBar.add(fileMenu);
		menuBar.add(editMenu);
		menuBar.add(helpMenu);

		setJMenuBar(menuBar);

		// 构建弹出菜单
		popupMenu = new JPopupMenu();
		popupMenu.add(cutAction);
		popupMenu.add(copyAction);
		popupMenu.add(pasteAction);

		JPanel popupPanel = new JPanel();
		// 显示弹出菜单
		popupPanel.setComponentPopupMenu(popupMenu);
		getContentPane().add(popupPanel);

		popupPanel.addMouseListener(new MouseAdapter() {
		});
	}

	/**
	 * 
	 * @author 不落的太阳(Sean Yang aka ShortPeace)
	 * @version 1.0
	 * @since jdk 1.8
	 *
	 */
	private class MenuAction extends AbstractAction {

		private static final long serialVersionUID = 978766943631469672L;

		public MenuAction(String name) {
			super(name);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println(getValue(Action.NAME) + " is selected");
		}
	}
}